package dbexp.model;

import dbexp.model.db.DbOT;

public class Synonym extends DbObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String LABEL = "synonimy";
	public static final String DEF_ICON = "synonym-icon";
	private String table_owner = "to";
	private String table_name = "tn";
	private String object_type = "ot";
	private String db_link = "dbl";

	public Synonym() {
		super(LABEL, DbOT.SYNONYMS);
		setIcons(DEF_ICON);
	};

	public Synonym(String name) {
		super(name, DbOT.SYNONYM);
		setIcons(DEF_ICON);
		set("leaf", true);
	}

	public Synonym(String name, String schema) {
		this(name);
		setSchema(schema);
	}

	public String getTableOwner() {
		return get(table_owner);
	}

	public void setTableOwner(String tableOwner) {
		set(table_owner, tableOwner);
	}

	public String getTableName() {
		return get(table_name);
	}

	public void setTableName(String tableName) {
		set(table_name, tableName);
	}

	public String getDbLink() {
		return get(db_link);
	}

	public void setDbLink(String dbLink) {
		set(db_link, dbLink);
	}

	public String getObjectType() {
		return get(object_type);
	}

	public void setObjectType(String objectType) {
		set(object_type, objectType);
	}

	private String getQuery(String oper) {
		if (requiredFieldIsNotNull()) {
			String query = "SYNONYM " + getObjectNameForQuery() + "\n"
					+ " FOR " + getTableOwner() + "." + getTableName();
			return oper + query;
		} else {
			return oneFromDual();
		}
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE OR REPLACE ");
		} else if (operation == DELETE) {
			return "DROP SYNONYM " + getObjectNameForQuery();
		}
		return oneFromDual();
	}

}
